var resLoader = require('resLoader')
const pi = 3.1415926
var UserData = require('UserData')
cc.Class({
    extends: cc.Component,
    properties: {
        skillShowTab:{
            default:null,
            type:cc.Prefab,
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        //this.node.cascadeOpacity =false
        this.getComponent()        
        this.node.Choose = 0
        this.button.active = false
        this.runIntoSceneAnimation(cc.v2(0,0))
        this.drawMap(this.map,this.radarMapData)
    },
    drawMap(map ,dataArr){
        let graphics = map.getComponent(cc.Graphics)        
        let radius = 150
        graphics.LineJoin = cc.ROUND
        graphics.LineCap = cc.ROUND
        graphics.fillColor = new cc.Color(0,0,200,255)
        graphics.moveTo(0,radius)
        let angle = 2*pi / dataArr.length 

        //数据
        let per = 0
        for(let i=0;i<dataArr.length;i++){
            per+=dataArr[i]
        }
        per = per /1200
        per = this.easeInOutCubic(per)
        graphics.fillColor = new cc.Color((1-per) *255 ,per * 255,0,150)
        graphics.strokeColor = new cc.Color((1-per) *255*0.7 ,per * 255*0.7,0,200)
        graphics.lineWidth = 3
        let ratio = radius /100
        graphics.moveTo(0,dataArr[0] *ratio )
        for(let i=0;i<dataArr.length;i++){
            graphics.lineTo(Math.sin(angle * (i)) * dataArr[i] *ratio,Math.cos(angle * (i))* dataArr[i] *ratio)
        }
        graphics.fill()
        graphics.lineTo(0,dataArr[0] *ratio)        
        
        graphics.stroke()
    },
    easeInOutCubic: function(pos){
        if ((pos/=0.5) < 1) return 0.5*Math.pow(pos,3);
        return 0.5 * (Math.pow((pos-2),3) + 2);
    },
    loadSkill(){
        this.skillShowList = this.node.getChildByName('技能列表').getChildByName('view').getChildByName('content')
        for(var i=0;i<this.chefData.skillList.length;i++){
            let skillBar = cc.instantiate(this.skillShowTab)
            this.resLoader.localImgLoader('Skill/' + this.chefData.skillList[i].skillID, skillBar.getChildByName('Skill Logo').getComponent(cc.Sprite))
            let str = "<color=#7B557C>" +"Lv." + this.chefData.skillList[i].levelGet + "解锁 </c>" 
            switch(this.chefData.skillList[i].target){
                case "self": str += "<color=#7B557C>主厨所在餐厅收益 </c>"
                break
                case "red": str += "<color=#FF3333>红色餐厅</c><color=#FF0000><color=#7B557C>收益 </c>" 
                break
                case "yellow": str += "<color=#FFFF33>黄色餐厅</c><color=#FF0000><color=#7B557C>收益 </c>" 
                break
                case "blue": str += "<color=#3333FF>红色餐厅</c><color=#FF0000><color=#7B557C>收益 </c>" 
                break
                default:
            }
            str += "<color=#7B557C>x" + this.chefData.skillList[i].effectValue + "</c>" 
            skillBar.getChildByName('skillEffect').getComponent(cc.RichText).string = str
            skillBar.getChildByName('skillName').getComponent(cc.Label).string = this.chefData.skillList[i].name? this.chefData.skillList[i].name:"skillName"
            
            this.skillShowList.insertChild(skillBar)
        }
    },
    getComponent(){
        this.stamp = this.node.getChildByName('stamp')
        this.button = this.node.getChildByName('button')
        this.randomIDLabel = this.node.getChildByName('编号')
        this.storyBoard = this.node.getChildByName('故事描述')
        this.photo = this.node.getChildByName('Photo')
        this.line = this.node.getChildByName('对话泡泡').getChildByName('台词')
        this.story = this.node.getChildByName('故事描述')
        this.nameLabel = this.node.getChildByName('Name Label')
        this.resLoader = resLoader
        this.map = this.node.getChildByName('12维图').getChildByName('map')
    },
    runIntoSceneAnimation(pos){
        this.node.rotation = -90
        this.node.x = -800
        this.node.y = 300
        var runInActionMove = cc.moveTo(1,pos)
        var runInActionRotate = cc.rotateTo(1,0)
        if(this.lastLetter){
            this.node.runAction(cc.sequence(cc.spawn(runInActionMove,runInActionRotate).easing(cc.easeExponentialOut()),cc.callFunc(this.lastLetterAlert,this)))
            
        }else{
            this.node.runAction(cc.sequence(cc.spawn(runInActionMove,runInActionRotate).easing(cc.easeExponentialOut()),cc.callFunc(this.enableTouch,this)))
        }
    },
    lastLetterAlert(){
        this.node.on(cc.Node.EventType.TOUCH_START,function(event) {
            this.getChildByName('lastAlertLabel').stopAllActions()
            this.getChildByName('lastAlertLabel').active = true
            this.getChildByName('lastAlertLabel').runAction(cc.sequence(cc.fadeIn(0.3),cc.delayTime(1.5),cc.fadeOut(1.5)))
        },this.node)
        this.button.active = true
    },
    enableTouch(){
        this.button.active = true 
        this.node.originPos = cc.v2(this.node.x, this.node.y)
        this.node.offsetX = 0
        this.node.confirmDistant = 300
        UserData.runtimeState.shareWindow.popOut()        
        this.node.on(cc.Node.EventType.TOUCH_START,function(event) {
            this.Moving = true
            this.offsetX = this.x - this.originPos.x
            if(this.recoverAction){
                this.stopAction(this.recoverAction)
            }            
        },this.node)
        this.node.on(cc.Node.EventType.TOUCH_MOVE,function(event) {
            
            var delta = event.touch.getDelta()
            this.offsetX +=delta.x
            if(Math.abs(this.offsetX)<this.confirmDistant){
                this.x = this.originPos.x+this.offsetX
            }
            this.y += delta.y /5
            this.rotation = (this.x - this.originPos.x)/10
            
        },this.node)

        this.node.on(cc.Node.EventType.TOUCH_END,()=> {
            this.runAway.call(this.node)
        },this)
       
        this.node.on(cc.Node.EventType.TOUCH_CANCEL,()=> {
            this.runAway.call(this.node)
        },this)
    },
    runAway(){
        this.recoverTime = Math.abs(this.x -this.originPos.x)/this.confirmDistant *2
        if(Math.abs(this.offsetX)<this.confirmDistant){
            var rotateAction = cc.rotateTo(this.recoverTime, 0)
            var moveAction = cc.moveTo(this.recoverTime, this.originPos)
            this.recoverAction = cc.spawn(rotateAction,moveAction).easing(cc.easeExponentialOut())
            this.runAction(this.recoverAction)
        }else{
            if(!UserData.runtimeState.muteSound){
                this.getComponent(cc.AudioSource).play()
            }
            
            this.targetOff(this)
            let direction = 0
            if(this.offsetX>0){ 
                direction = 1
                this.Choose = -1                
            }else{
                direction = -1
            }
            var rotateAction = cc.rotateTo(this.recoverTime*2, 180 * direction )
            var moveAction = cc.moveTo(this.recoverTime*2, cc.v2(this.confirmDistant*4 * direction,this.y))
            this.runawayAction = cc.spawn(rotateAction,moveAction).easing(cc.easeExponentialOut())
            this.runAction(this.runawayAction)
        }    
    },
    pressButton(){
        this.node.targetOff(this.node)
        this.button.getComponent(cc.Button).interactable =false
        this.stamp.active = true
        this.stamp.scaleX = 30
        this.stamp.scaleY = 30
        let stampAction = cc.scaleTo(0.5,1).easing(cc.easeExponentialIn())
        this.button.active =false
        this.stamp.runAction(cc.sequence(stampAction,cc.callFunc(
            ()=>{ this.node.getComponent('Shake').shake(0.5)},this
        ),cc.delayTime(2),cc.callFunc(this.sendMsg,this)))
    },
    sendMsg(){
        this.res.getComponent('Recruit').recruitConfirm(this.node)
    },
    //入口
    loadChefData(chefData){
        this.chefData = chefData
        this.getComponent()
        this.resLoader.plistLoader("Chef/Photo/主厨plist" , this.chefData.basicData.chefID ,this.photo.getComponent(cc.Sprite))
        //this.loadSkill()
        this.line.getComponent(cc.Label).string = this.chefData.basicData.lines? this.chefData.basicData.lines : '来尝尝我的手艺吧！'
        this.story.getComponent(cc.Label).string = this.chefData.basicData.description
        this.nameLabel =this.chefData.basicData.name
        
        this.radarMapData  = [40,51,31,21,11,1,22,33,44,55,66,77]
        for(let i=0;i<12;i++){
            this.radarMapData[i] = parseInt((Math.random()*100).toFixed(0)) //待修改能力值分布
        }
        this.chefData.chefRadar =
        [
            {name:"FASTFOOD",       value:this.radarMapData[0]},
            {name:"ICECREAM",       value:this.radarMapData[1]},
            {name:"CAKE",           value:this.radarMapData[2]},
            {name:"MILKYTEA",       value:this.radarMapData[3]},
            {name:"COFFEE",         value:this.radarMapData[4]},
            {name:"JPFOOD",         value:this.radarMapData[5]},
            {name:"BARBECUE",       value:this.radarMapData[6]},
            {name:"BAR",            value:this.radarMapData[7]},
            {name:"HOTPOT",         value:this.radarMapData[8]},
            {name:"SEAFOOD",        value:this.radarMapData[9]},
            {name:"WESTERN",        value:this.radarMapData[10]},
            {name:"HOMECOOKING",    value:this.radarMapData[11]},

        ]
        
    }

});
